﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.DanhMuc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.DanhMuc;
using Webdiyer.WebControls.Mvc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.DanhMucDelegate
{
    public class DanTocDelegate
    {
        DBDanhMucDataContext DBDanhMuc = new DBDanhMucDataContext();

        public IList<dm_DanToc> GetAllDanToc()
        {
            return DBDanhMuc.dm_DanTocs.Where(p => p.IsDelete == false).ToList();
        }
        public dm_DanToc AddNewDanToc(dm_DanToc DanToc)
        {
            DBDanhMuc.dm_DanTocs.InsertOnSubmit(DanToc);

            DBDanhMuc.SubmitChanges();

            return GetLoaiCongChucIdMax();
        }

        public void DeleteDanToc(int DanTocID)
        {
            dm_DanToc DanTocNew =
                DBDanhMuc.dm_DanTocs.Single(p => p.Id == DanTocID);
            //LoaiCongChuc.Id = LoaiCongChuc.Id;

            DBDanhMuc.dm_DanTocs.DeleteOnSubmit(DanTocNew);
            DBDanhMuc.SubmitChanges();
        }
        public int DeleteDanToc(dm_DanToc DanToc)
        {
            dm_DanToc DanTocDelete =
                DBDanhMuc.dm_DanTocs.Single(p => p.Id == DanToc.Id);
            if (DanTocDelete.hs_NhanViens.Count() == 0)
            {
                DanTocDelete.Id = DanToc.Id;
                DanTocDelete.UpdateDate = DateTime.Now;
                DanTocDelete.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
                DanTocDelete.IsDelete = true;
                DBDanhMuc.SubmitChanges();
                return 1;
            }
            else
            {
                return 0;
            }
        }
        public void UpdateDanToc(dm_DanToc DanToc)
        {
            dm_DanToc DanTocUpdate =
                DBDanhMuc.dm_DanTocs.Single(p => p.Id == DanToc.Id);

            DanTocUpdate.Id = DanToc.Id;
            DanTocUpdate.TenDanToc = DanToc.TenDanToc;
            DanTocUpdate.UpdateDate = DanTocUpdate.UpdateDate;
            DanTocUpdate.UpdateBy = DanTocUpdate.UpdateBy;
            DBDanhMuc.SubmitChanges();
        }

        public PagedList<dm_DanToc> SearchDanTocPagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dm_DanToc>)DBDanhMuc.dm_DanTocs.Where(p => p.TenDanToc.Contains(searchCriteria) && p.IsDelete == false).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);


        }

        public dm_DanToc GetDanTocById(string id)
        {
            try
            {
                dm_DanToc item = this.DBDanhMuc.dm_DanTocs.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_DanToc GetDanTocByTenCheck(string ten)
        {

            IEnumerable<dm_DanToc> list = this.DBDanhMuc.dm_DanTocs.Where(p => p.TenDanToc == ten && p.IsDelete == false);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public dm_DanToc GetDanTocByTen(string ten)
        {

            IEnumerable<dm_DanToc> list = this.DBDanhMuc.dm_DanTocs.Where(p => p.TenDanToc == ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public PagedList<dm_DanToc> RecycleGetDanTocPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_DanTocs.Where(p => p.IsDelete == true).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public PagedList<dm_DanToc> GetDanTocPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_DanTocs.Where(p => p.IsDelete == false).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_DanToc BuildNewDanToc(DanTocForm form)
        {
            dm_DanToc newDanToc = new dm_DanToc();
            newDanToc.TenDanToc = form.TenDanToc;
            newDanToc.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newDanToc.CreateDate = DateTime.Now;
            newDanToc.IsDelete = false;
            return newDanToc;
        }
        public int PhucHoiDanToc(int id)
        {
            dm_DanToc phuchoi = new dm_DanToc();
            phuchoi = this.GetDanTocById(Convert.ToString(id));
            dm_DanToc item = new dm_DanToc();
            item = this.GetDanTocByTenCheck(phuchoi.TenDanToc);
            if (item != null)
                return 1;
            else
            {
                phuchoi.IsDelete = false;
                DBDanhMuc.SubmitChanges();
                return 2;
            }
        }
        public dm_DanToc BuildEditDanToc(DanTocForm form)
        {
            dm_DanToc newDanToc = new dm_DanToc();
            newDanToc.Id = form.id;
            newDanToc.TenDanToc = form.TenDanToc;
            newDanToc.UpdateDate = DateTime.Now;
            newDanToc.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newDanToc;
        }
        public dm_DanToc GetLoaiCongChucIdMax()
        {
            IList<dm_DanToc> list = DBDanhMuc.dm_DanTocs.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public DanTocForm ConvertToForm(dm_DanToc obj)
        {
            DanTocForm form = new DanTocForm();
            form.id = obj.Id;
            form.TenDanToc = obj.TenDanToc;
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }
    }
}